package noname;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import model.*;
import view.*;

public class Controller {

    private volatile static Controller instance;
    
    private Map map;
    private Route route;
    private GpsReceiver gpsReceiver;
    private AbstractDistanceCalculator distanceCalculator;
    private PointOfInterestWatcher watcher;
    private MainView mainView;
    private MultimediaControl multimediaController;
    
    public Controller() {

        try {
            // 1.) Create models

            // Maak de map
            String imgPath = ConfigSettings.getInstance().getMapImagePath() + "map1.jpg";
            BufferedImage mapImg = ImageIO.read(new File(imgPath));
            GpsCoordinate refTopLeft = new GpsCoordinate(
                    new GpsLatitude(51, 35, 39.14),
                    new GpsLongitude(4, 46, 17.62));
            GpsCoordinate bottomRight = new GpsCoordinate(
                    new GpsLatitude(51, 35, 12.44),
                    new GpsLongitude(4, 47, 2.86));

            map = new Map(mapImg, refTopLeft, bottomRight);

            // Maak de route
            route = new Route();

            // maak GPS receiver
            gpsReceiver = new GpsReceiver();
            
            // Maak distance calculator
            distanceCalculator = new MetrischDistanceCalculator();

            // Maak point of interest watcher
            watcher = new PointOfInterestWatcher(gpsReceiver, route);

            // 2.) Create views

            // Maak de map view met decorators
            JComponent mapView = new MapView(map);
            JComponent routeView = new RouteViewDecorator(mapView, map, route);
            JComponent poiMarkersViewDecorator = new PoiMarkersViewDecorator(routeView, map, route);
            JComponent PositionMarkerViewDecorator = new PositionMarkerViewDecorator(poiMarkersViewDecorator, gpsReceiver, map);
            JComponent NextPointOfInterestViewDecorator = new NextPointOfInterestViewDecorator(PositionMarkerViewDecorator, map, watcher);
            JComponent currentPoiInfoView = new CurrentPoiInfoViewDecorator(NextPointOfInterestViewDecorator, map, watcher, distanceCalculator, gpsReceiver);

            // Voeg de view toe aan de main view
            mainView = new MainView(this);
            mainView.setMainView(currentPoiInfoView);
            mainView.setVisible(true);

            // 3.) Maak de multimedia controller
            
            // Maak de multimedia controller
            multimediaController = new MultimediaControl(mainView.getMultimediaPanel(), watcher);
            
        } catch (Exception ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
